const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 音乐列表
    list: [],
    page: 1,
    limit: 10,
    name: '',

    // 列表哪一个
    number: '-1',

    // 显示播放暂停
    status: true,

    statustime: '00:00',
    sumtime: '',
    width: '0%',
    music: '',
  },
  delete: function () {
    this.setData({
      name: ''
    })
    this.musiclist()
  },
  // goaudio: function (e) {
  //   wx.navigateTo({
  //     url: '../Amusicdetails/index',
  //   })
  // },

  name: function (e) {
    console.log(e)


    var that = this
    that.setData({
      name: e.detail.value,
      page:'1'
    })

    that.musiclist();
  },


  audioPlay(e) {


    console.log(e);
    this.setData({
      number: e.currentTarget.dataset.index,
      status: false,
      music: e.currentTarget.dataset.music,
      width: '0%',
    })
    console.log(this.data.number)

    this.innerAudioContext.destroy()

    if (!e.currentTarget.dataset.music) {
//    wx.showToast({
//      title: '音频地址错误',
//      icon: "none",
//      duration: 2000
//    })
      return false;
    }


    wx.showLoading({
      title: '正在缓冲',
    })
    var that = this
    this.innerAudioContext = wx.createInnerAudioContext()
    this.innerAudioContext.autoplay = true
    this.innerAudioContext.src = this.data.music
    this.innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    setTimeout(() => {
      this.innerAudioContext.onTimeUpdate(() => {
        console.log(this.innerAudioContext.duration)  //总时长
        console.log(this.innerAudioContext.currentTime)  //当前播放进度
        let sumtime = this.innerAudioContext.duration;
        let newtime = this.innerAudioContext.currentTime
        let width = newtime / sumtime * 100 + '%'
        console.log(width)
        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);


        // 进度时间
        var newtimem = parseInt(newtime / 60);
        var newtimes = parseInt(newtime % 60);
        if (newtimem < 10) {
          newtimem = "0" + newtimem
        }
        if (newtimes < 10) {
          newtimes = "0" + newtimes
        }


        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)
        that.setData({
          width: width,
          sumtime: end,
          statustime: start,
        })
      })
    }, 500)

    this.innerAudioContext.onPlay(() => {
      wx: wx.hideLoading()
      that.setData({
        buttonstatus: false
      })
    })

    //  自然播放完后
    this.innerAudioContext.onEnded(() => {
      that.setData({
        // 列表哪一个
        number: '-1',

        // 显示播放暂停
        status: true,

        statustime: '00:00',
        sumtime: '',
        width: '0%',
      })
    })


  },
  audioPause(e) {
    console.log(e);
    this.innerAudioContext.pause()
    this.setData({
      number: '-1'
    })


  },






  // 获取列表
  musiclist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
          name: that.data.name,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.pgoods,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list,
                })
                console.log("aaa")
                wx.hideLoading();
                // 空的
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                wx.hideLoading()
                console.log(list)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }


            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail(e){
        var setdata = {
          page: that.data.page,
          limit: that.data.limit,
          name: that.data.name,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.pgoods,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list,
                })
                console.log("aaa")
                wx.hideLoading();
                // 空的
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                wx.hideLoading()
                console.log(list)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }


            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '../login/login'
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.musiclist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.innerAudioContext = wx.createInnerAudioContext()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.innerAudioContext.destroy()
    this.setData({
      // 显示播放暂停
      number: '-1',

      statustime: '00:00',
      sumtime: '',
      width: '0%',
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    var page = that.data.page + 1
    that.setData({
      page: page
    })
    this.musiclist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})